package cn.gson.crm.controller.custom;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.CustomDao;
import cn.gson.crm.model.entity.Custom;
import cn.gson.crm.vo.CustomVo;

/**
 * 新建潜在客户
 */
@WebServlet("/custom/form")
public class CustomFormServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private CustomDao customDao = new CustomDao();
	private String jsp = "custom/custom_form.jsp";
	List<Map<String, Object>> degreeList = null;

	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		degreeList = customDao.getDegreeAll();
		request.setAttribute("degreeList", degreeList);
		forward("custom/custom_form.jsp");
	}

	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String Name = request.getParameter("name");
		String Gender = request.getParameter("gender");
		String Tel = request.getParameter("tel");
		String QQ = request.getParameter("QQ");
		String ParentName = request.getParameter("parentName");
		String ParentTel = request.getParameter("parentTel");
		String Source = request.getParameter("source");
		// String Counselor = request.getParameter("counselor");
		String Degree = request.getParameter("degree");
		String Address = request.getParameter("address");

		Custom custom = new Custom();
		custom.setCustomName(Name);
		custom.setCustomGender(Gender);
		custom.setCustomAddress(Address);
		custom.setCustomTel(Tel);
		if (Degree != "" || !Degree.isEmpty()) {
			custom.setCustomDegreeId(Integer.parseInt(Degree));
		}
		custom.setCustomQQ(QQ);
		custom.setCustomParentName(ParentName);
		custom.setCustomParentTel(ParentTel);
		custom.setCustomSource(Source);
		// if (Counselor != "" || !Counselor.isEmpty()) {
		// custom.setCustomCounselorId(Integer.parseInt(Counselor));
		// }
		
		if (Name == null || Name.isEmpty()) {
			turn(request, custom);
			errorForward("姓名不能为空", jsp);
			return;
		}else{
			Pattern pattern = Pattern.compile("[\u4E00-\u9FA5]{2,4}");
			Matcher mqq = pattern.matcher(Name);
			boolean bqq = mqq.matches();
			if (!bqq) {
				turn(request, custom);
				errorForward("学生姓名格式不对", jsp);
				return;
			}
		}
		if (Gender == null || Gender.isEmpty()) {
			turn(request, custom);
			errorForward("性别不能为空", jsp);
			return;
		}
		if (Tel == null || Tel.isEmpty()) {
			turn(request, custom);
			errorForward("电话不能为空", jsp);
			return;
		} else {
			Pattern patternTell = Pattern.compile("^1[0-9]\\d{9}$");
			Matcher mTell = patternTell.matcher(Tel);
			boolean bTell = mTell.matches();
			if (!bTell) {
				turn(request, custom);
				errorForward("电话格式不对", jsp);
				return;
			}
		}

		if (QQ == null || QQ.isEmpty()) {
			turn(request, custom);
			errorForward("qq不能为空", jsp);
			return;
		}else{
			Pattern pqq = Pattern.compile("[1-9][0-9]{4,14}");
			Matcher mqq = pqq.matcher(QQ);
			boolean bqq = mqq.matches();
			if (!bqq) {
				turn(request, custom);
				errorForward("QQ格式不对", jsp);
				return;
			}
		}
		if (ParentName == null || ParentName.isEmpty()) {
			turn(request, custom);
			errorForward("家长姓名不能为空", jsp);
			return;
		}else{
			Pattern pattern = Pattern.compile("[\u4E00-\u9FA5]{2,4}");
			Matcher mqq = pattern.matcher(ParentName);
			boolean bqq = mqq.matches();
			if (!bqq) {
				turn(request, custom);
				errorForward("学生家长姓名格式不对", jsp);
				return;
			}
		}
		if (ParentTel == null || ParentTel.isEmpty()) {
			turn(request, custom);
			errorForward("家长电话不能为空", jsp);
			return;
		} else {
			Pattern patternTell = Pattern.compile("^1[0-9]\\d{9}$");
			Matcher mTell = patternTell.matcher(ParentTel);
			boolean bTell = mTell.matches();
			if (!bTell) {
				turn(request, custom);
				errorForward("家长电话格式不对", jsp);
				return;
			}
		}

		if ((Tel != null && !Tel.isEmpty()) && (ParentTel != null && !ParentTel.isEmpty())) {
			if (Tel.equals(ParentTel)) {
				turn(request, custom);
				errorForward("客户电话和客户家长电话不能一样", jsp);
				return;
			}
		}
		if (Source == null || Source.isEmpty()) {
			turn(request, custom);
			errorForward("来源不能为空", jsp);
			return;
		}
		if (Degree == null || Degree.isEmpty()) {
			turn(request, custom);
			errorForward("学历不能为空", jsp);
			return;
		}
		if (Address == null || Address.isEmpty()) {
			turn(request, custom);
			errorForward("地址不能为空", jsp);
			return;
		}

		// 验证
		// 判断邮箱的是不是符合邮箱格式
		

		boolean result = customDao.insertCustom(custom);
		if (result) {
			redirect("custom");
		} else {
			request.setAttribute("custom", custom);
			errorForward("保存失败", jsp);
		}
	}

	private void turn(HttpServletRequest request, Custom custom) {
		request.setAttribute("degreeList", degreeList);
		request.setAttribute("custom", custom);
	}

}
